use std::sync::Arc;

use crate::AppState;
use crate::controller::Controller;
use crate::pojo::{req::UserReq, vo::UserVO};
use axum::{
    Json, Router,
    extract::{Extension, Path, State},
    routing::{get, post},
};
use common::base::{biz_error::AppError, dto::SysUser, res::Res};
pub struct UserController;
impl Controller for UserController {
    fn router() -> Router<AppState> {
        Router::new()
            .route("/{id}", get(query_by_id))
            .route("/err", get(do_err))
            .route("/login", post(login))
    }
}

async fn query_by_id(
    State(status): State<AppState>,
    Path(id): Path<i64>,
) -> Result<Json<Res<UserVO>>, AppError> {
    Ok(Json(status.user_service.query_by_id(id).await))
}

async fn do_err(State(status): State<AppState>) -> Result<Json<Res<String>>, AppError> {
    Ok(Json(status.user_service.do_err()))
}

async fn login(
    State(status): State<AppState>,
    Json(user): Json<UserReq>,
) -> Result<Json<Res<UserVO>>, AppError> {
    Ok(Json(status.user_service.login(user).await))
}
